cmake_minimum_required(VERSION 3.14.3)

project(tracker_kcf C CXX ASM)

enable_language(ASM)

set(CMAKE_DEBUG_TARGET_PROPERTIES
        INCLUDE_DIRECTORIES
        COMPILE_DEFINITIONS
        POSITION_INDEPENDENT_CODE
        CONTAINER_SIZE_REQUIRED
        LIB_VERSION
    )

include(${HEXAGON_CMAKE_ROOT}/hexagon_fun.cmake)

set(CMAKE_VERBOSE_MAKEFILE ON)

set(common_incs
    ${CMAKE_CURRENT_BINARY_DIR}
    ${HEXAGON_SDK_ROOT}/incs/
    ${HEXAGON_SDK_ROOT}/incs/stddef/
    ${HEXAGON_SDK_ROOT}/utils/examples/
    ${HEXAGON_SDK_ROOT}/ipc/fastrpc/rpcmem/inc/
)
include_directories(${common_incs})

if(${OS_TYPE} MATCHES "HLOS")

    add_library(tracker_kcf SHARED ${CMAKE_CURRENT_BINARY_DIR}/tracker_kcf_stub
        ${HEXAGON_SDK_ROOT}/utils/examples/dsp_capabilities_utils
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/tracker_kcf_dsp_adap.c
        )

    build_idl(inc/tracker_kcf.idl tracker_kcf)

    choose_dsprpc(${DSP_TYPE} dsprpc)

    link_custom_library(tracker_kcf rpcmem)
    link_custom_library(tracker_kcf ${dsprpc})
    if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
        target_compile_definitions(tracker_kcf PUBLIC VERIFY_PRINT_ERROR)
        target_link_options(tracker_kcf PUBLIC -llog)
    endif()

    set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src_app/3rd/opencv-4.5.5-android-sdk/OpenCV-android-sdk/sdk/native/jni)
    find_package(OpenCV REQUIRED)
    message("--+++++++++++${OpenCV_LIBS}")
    include_directories(${OpenCV_DIR}/include)
    # HLOS executable
    add_executable(tracker_kcf_device
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/test_main.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/test_cpu_main.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/test_dsp_main.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/TrackerKCF.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/TrackerKCFdsp.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_app/utils.cpp
    )

    # Add common HLOS-specific linker options
    link_options(tracker_kcf_device)

    set_target_properties(tracker_kcf_device PROPERTIES OUTPUT_NAME "tracker_kcf")

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -std=c++14")
    # Common linking for all the executables that are build
    if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
        target_compile_definitions(tracker_kcf_device PUBLIC VERIFY_PRINT_ERROR)
        target_link_options(tracker_kcf_device PUBLIC -llog -lc -lz -lm -ldl -lc++_shared )
        target_link_libraries(tracker_kcf_device ${OpenCV_LIBS})
    endif()

    link_custom_library(tracker_kcf_device ${dsprpc})
    link_custom_library(tracker_kcf_device rpcmem)

    add_dependencies(tracker_kcf_device tracker_kcf)
    target_link_libraries(tracker_kcf_device tracker_kcf)
    # Copy binaries from ship to CMAKE_CURRENT_BINARY_DIR folder
    copy_binaries(tracker_kcf_device)

else()

    # All the skel libraries to be build
    set(KCF_DSP "tracker_kcf_skel")

    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/myfft)

    # Queue test skeleton
    add_library(${KCF_DSP} SHARED
        ${CMAKE_CURRENT_BINARY_DIR}/tracker_kcf_skel

        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/tracker_kcf_imp.cpp

        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/dsp_intrinsics.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/fastcv_aux.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/FFTTools.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/TrackerKCF.cpp

        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/qhblas_hvx_uw_vector_dot_aub.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/transpose_int64.c

        ${CMAKE_CURRENT_SOURCE_DIR}/src_dsp/qhblas_vector_hadamard_conj_acf.S
    )
    
    # target_compile_definitions(tracker_kcf_skel PUBLIC KCF_VERSION)
    build_idl(inc/tracker_kcf.idl ${KCF_DSP})

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexceptions -std=c++14")

    target_link_libraries(${KCF_DSP} ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++abi.so.1)
    target_link_libraries(${KCF_DSP} ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++.so.1 )

    link_custom_library(${KCF_DSP} qhl)
    link_custom_library(${KCF_DSP} qhl_hvx)
    link_custom_library(${KCF_DSP} worker_pool)

    #With Hexagon Tools 8.4.0x, new symbols are added for c++17 compliance,
    #whose definitions are not present on targets earlier to saipan.
    #Link this weak_refs.so to your library for weak definitions of these new symbols.
    string(FIND ${PREBUILT_LIB_DIR} "toolv81" is_toolv81)
    string(FIND ${PREBUILT_LIB_DIR} "toolv82" is_toolv82)
    string(FIND ${PREBUILT_LIB_DIR} "toolv83" is_toolv83)

    if (${is_toolv81} EQUAL -1 AND ${is_toolv82} EQUAL -1 AND ${is_toolv83} EQUAL -1)
        target_link_libraries(${KCF_DSP} ${HEXAGON_SDK_ROOT}/libs/weak_refs/ship/hexagon_toolv84/weak_refs.a)
    endif()

    file(COPY ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++abi.so.1 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++.so.1 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++abi.so.1.0 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${HEXAGON_LIB_DIR}/${HEXAGON_ARCH}/G0/pic/libc++.so.1.0 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})


    # Copy binaries from ship to CMAKE_CURRENT_BINARY_DIR folder
    copy_binaries(${KCF_DSP})

endif()
